
#ifndef XDNS_SERVER_STATE_H
#define XDNS_SERVER_STATE_H

#include "xdns.h"
#include <set>

class XdnsData;
class XdnsHeaderInfo;
class XdnsQuery;
class XdnsServerStateListener;

class XdnsServerState
{
public:
    /*! XdnsServerState constructor

        @param port DNS server port for both TCP and UDP, the default value is 53.
        @param address Internet host address of DNS server.
    */
    XdnsServerState(unsigned short port,
                    const struct in_addr* address);
    virtual ~XdnsServerState();

    static XdnsError Init(unsigned short port,
                          const struct in_addr* address,
                          XdnsServerState** serverState);
    static void Release(XdnsServerState* serverState);

    void RegisterListener(XdnsServerStateListener& listener);
    void UnregisterListener(XdnsServerStateListener& listener);

    /* See xdns-server-manager.h for description. */
    virtual XdnsError SendQuery(long queryId,
                                const XdnsQuery& query
                                ) = 0;

    /* See xdns.h for description. */
    virtual void CancelQuery(long queryId) = 0;
    virtual XdnsError GetProcessingFds(int* nfds,
                                       fd_set* readfds,
                                       fd_set* writefds
                                       ) = 0;
    virtual XdnsError Process(fd_set* readfds,
                              fd_set* writefds
                              ) = 0;

protected:
    void NotifyListeners(long queryId,
                         XdnsError error,
                         const XdnsData* data,
                         size_t startOffset,
                         const XdnsHeaderInfo* header);

    unsigned short m_port;
    struct in_addr m_address;
    std::set<XdnsServerStateListener*> m_listeners;
};

#endif  // XDNS_SERVER_STATE_H

